<?php
class CartAction extends MbaseAction {
	public $tbname = 'Cart';
	public $obj = '';
	public $mpk = 'cart_id';
	public $cate = '';
	public function index(){
		$cartinfo = cookie('mycart');
		$cartinfo = json_decode(stripcslashes($cartinfo),true);
		$goodlist = $cartinfo['good'];
		$member_id = isset($_SESSION['member_id'])? $_SESSION['member_id']:0;
		$this->assign('iscart',1);
		$this->assign('isindex',1);
		
		$freight_fee = $this->getConfig('freight_fee');
		$get_rate = $this->getConfig('getrate');
		
		$allprice = 0;
		foreach($goodlist as $adata){
			$allprice += $adata['total'];
		}
		
		//$allprice =round($freight_fee+$allprice,2);
		$this->assign('freight_fee',$freight_fee);
		$this->assign('total_fee',$allprice);

		$final_fee = $allprice+$freight_fee;
		$this->assign('final_fee',$final_fee);
		$this->assign('datalist',$goodlist);
		$this->display();
	}
	
	public function create(){
		$cartinfos = cookie('mycart');
		if(empty($cartinfos)){
			$cartinfos = array(
				'good' => array()
			);
		}else{
			$cartinfos = json_decode(stripcslashes($cartinfos),true);
		} 
		$newcart = array();
		
		$member_id = isset($_SESSION['member_id'])? $_SESSION['member_id']:0;
		$good_id = isset($_POST['good_id'])? $_POST['good_id'] : 0;
		$good_num  = isset($_POST['good_num'])? $_POST['good_num'] : 0;
		$good_attr = isset($_POST['good_attr'])? $_POST['good_attr'] : '';
		
		$gobuy = isset($_POST['gobuy'])? (int)$_POST['gobuy'] : 0;
		
		$good_attr_val = '';
		if(is_array($good_attr)){
			$good_attr_val = implode(";", $good_attr);
		}
		if(!$good_id) $this->error('无商品记录',1);
		if(!$good_num) $this->error('购买数量不能为空',1);
		$mgood = D('Good');
		$mcart = D('Cart');
		$gmap['good_id'] = $good_id;
		$goodinfo = $mgood->where($gmap)->find();
		if(!$goodinfo)  $this->error('商品缺货',1);
		
		$good_price = $goodinfo['shop_fee'];
		if($goodinfo['is_promote'] == 1) $good_price = $goodinfo['promote_fee'];
		//cart_id 	member_id 	good_id 	good_code 	good_name 	good_attr 	good_num 	good_price 
		$datainfo = array(
			'cart_id' => '',
			'member_id' => $member_id,
			'good_id' => $good_id,
			'good_code' => $goodinfo['code'],
			'good_name' => $goodinfo['name'],
			'good_img' => $goodinfo['thumb'],
			'good_attr' => $good_attr_val,
			'good_num' => $good_num,
			'good_price' => $good_price,
			'total' => $good_price*$good_num,
		);
		
		if(!empty($goodinfo['tcode'])) $datainfo['good_code'] = $goodinfo['tcode'];
		$cartinfos['good'][] = $datainfo;
		$cartinfosstr = json_encode($cartinfos);
		cookie('mycart',$cartinfosstr);
		if($gobuy == 1){
			$this->success('ok',1);
		}else{
			$vcart['good'] = count($cartinfos);
			$vcart['price'] = 100;
			$this->success('加入购物车成功！',$vcart);
		} 
		
	}
	
	public function del(){
		$cart_id  = isset($_POST['cart_id'])? $_POST['cart_id'] : 0;
		$cartinfo = cookie('mycart');
		$cartinfo = json_decode(stripcslashes($cartinfo),true);
		$goodlist = $cartinfo['good'];
		if(!isset($goodlist[$cart_id ]))  $this->error('无此记录',1);
		unset($cartinfo['good'][$cart_id]);
		$cartinfosstr = json_encode($cartinfo);
		cookie('mycart',$cartinfosstr);
		$this->success('删除成功！',$cart_id);
	}
}
?>